//app.js
import {Event} from './utils/Event';

App({
    event: new Event(),
    onLaunch: function () {
        wx.getSystemInfo({
            success: res => {
                //导航高度
                this.config.title_height = res.statusBarHeight + 46;
                this.config.statusbarHeight = res.statusBarHeight;

            }, fail(err) {
                console.log(err);
            }
        })
    },
    getAccountInfo() {
        /// 判断是否存在account，如果存在则获取token

        if(!wx.getStorageSync('username')){
            return
        }

        wx.request({
            url: this.httpsPrefix,
            data: {query: 'mp_userinfo', username: wx.getStorageSync('username')},
            success: ({data}) => {
                data = data.data[0]
                console.log(data)
                wx.setStorageSync('username', data["帐号"])
                wx.setStorageSync('account', data["帐号"])
                wx.setStorageSync('课题组编号', data["默认课题组编号"])
                wx.setStorageSync('姓名', data['姓名'])
                wx.setStorageSync('角色编号', data['默认角色编号'])
                wx.setStorageSync('token', data['token'])
            }
        })
    },
    post:function (url, data, dbValues = null) {
        var promise = new Promise((resolve, reject) => {
            //init
            let postData = data;
            if (dbValues !== null) {
                dbValues.forEach((v) => {
                    postData[v] = wx.getStorageSync(v)
                })
            }

            console.log(postData)
            //网络请求
            wx.request({
                url: url,
                data: postData,
                method: 'POST',
                header: {'content-type': 'application/x-www-form-urlencoded'},
                success: function ({data}) {//服务器返回数据
                    if (data.flag === '0' && data.message.indexOf('token') != -1) {
                        wx.showModal({
                            title: '提示',
                            content: '您的帐号在其他设备上登陆，是否重新登陆？',
                            success: function (res) {
                                if (res.confirm) {
                                    wx.navigateTo({
                                        url: '../login/login'
                                    })
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        })

                        return
                    }
                    resolve(data);
                },
                fail: function (e) {
                    reject('网络出错');
                }
            })
        });
        return promise;
    },
    httpsPrefix: "https://www.frontso.com/mpproxy?platform=ccmu",
    sourceUrl:'https://fysq.netdj.club/bdsyj2018/',

    config:{
        title_height: "64",
        statusbarHeight: "24",
        titleIcon_height: "32",
        titleIcon_width: "87",
        title_top: "24",
        title_text: "xxx", // iphone X + 24
        prefix: 24
    }
})
